<?php
//http://davidwalsh.name/gmail-php-imap

	include_once("email_parser.php");
	
	class GmailParser extends EmailParser{
		
		private $username = "";
		private $password = "";
		public $numberOfEmails = 20;
		
		//Should set a default url
		function __construct($user, $pass) {
			parent::__construct();
			$this->url = '{imap.gmail.com:993/imap/ssl}INBOX';
			$this->password = $pass;
			$this->username = $user;
		}
		
		function scrapePosts() {
			$inbox = imap_open($this->url,$this->username,$this->password) or die('Cannot connect to Gmail: ' . imap_last_error());
			$posts = array();
			$emails = imap_search($inbox, 'ALL');
			if($emails) {
				rsort($emails);
				for($i=0; $i<$this->numberOfEmails; $i++) {
					if($i >= count($emails))
						break;
					$email_number = $emails[$i];
					$overview = imap_fetch_overview($inbox,$email_number,0);
					$post = new Post();
					$post->rank = $i + 1;
					@$content = htmlspecialchars('Date: ' . $overview[0]->date . ' From: ' . $overview[0]->from . ' Subject: ' . $overview[0]->subject);
					$content = str_replace('&lt;', '(', $content); 
					$content = str_replace('&gt;', ')', $content); 
					$post->content = $content;
					array_push($posts, $post);
				}
			}
			
			return $posts;
		}
	}

?>
